package com.yj.cmp.sdk.fusioncompute.network.portgroup;

import com.yj.cmp.sdk.fusioncompute.common.utils.RequestUtils;
import com.yj.cmp.sdk.fusioncompute.network.portgroup.dto.PortGroupDto;
import com.yj.cmp.sdk.fusioncompute.network.portgroup.vo.PortGroupVo;
import com.yj.cmp.sdk.fusioncompute.network.portgroup.vo.PortGroupsListVo;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Component;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author Harvey_Yang
 * @date 2021/8/28 11:15
 */
@Component
public class FcPortGroupApi {
    FcPortGroupService fcPortGroupService;

    public FcPortGroupApi(FcPortGroupService fcPortGroupService) {
        this.fcPortGroupService = fcPortGroupService;
    }

    /**
     * 端口组列表
     */
    public List<PortGroupsListVo.PortGroupsDTO> portGroupsList(Map<String, Object> headerMap, Map<String, Object> queryParam) throws Exception {
        ArrayList<PortGroupsListVo.PortGroupsDTO> portGroupsDTOList = new ArrayList<>();
        Response<PortGroupsListVo> response = fcPortGroupService.portGroupsList(headerMap, queryParam).execute();
        RequestUtils.isSuccessful(response);
        if (response.body().getTotal() <= 100) {
            if (null != response.body()) {
                return response.body().getPortGroups();
            }
            return new ArrayList<>(0);
        }
        if (response.body().getTotal() != null) {
            int page = (int) Math.ceil(new Double(response.body().getTotal()) / 100);
            for (int i = 2; i <= page; i++) {
                queryParam.put("offset", i);
                queryParam.put("limit", 100);
                portGroupsDTOList.addAll(fcPortGroupService.portGroupsList(headerMap, queryParam).execute().body().getPortGroups());
            }
        }
        return portGroupsDTOList;
    }


    /**
     * 创建端口组
     */
    public PortGroupVo createPortGroup(Map<String, Object> headerMap, PortGroupDto portGroupDto) throws Exception {

        Response<PortGroupVo> response = fcPortGroupService.createPortGroup(headerMap, portGroupDto).execute();
        RequestUtils.isSuccessful(response);
        if (null != response.body()) {
            return response.body();
        }
        return null;
    }

    /**
     * 删除端口组
     */
    public int deletePortGroup(Map<String, Object> headerMap, String portGroupId) throws Exception {
        Response<ResponseBody> response = fcPortGroupService.deletePortGroup(headerMap, portGroupId).execute();
        RequestUtils.isSuccessful(response);
        return response.code();
    }

    /**
     * 修改端口组
     */
    public PortGroupVo updatePortGroup(Map<String, Object> headerMap, String portGroupId,
                                       PortGroupDto portGroupDto) throws Exception {
        Response<PortGroupVo> response = fcPortGroupService.updatePortGroup(headerMap, portGroupId, portGroupDto).execute();
        RequestUtils.isSuccessful(response);
        if (null != response.body()) {
            return response.body();
        }
        return new PortGroupVo();
    }
}
